package com.fitness.fitness.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.fitness.mapper.*;
import com.fitness.fitness.modal.datavisual.entity.*;
import com.fitness.fitness.service.IDataVisualService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DataVisualServiceImpl extends ServiceImpl<RateVisualMapper, RateVisual> implements IDataVisualService {

    private final RateVisualMapper rateVisualMapper;
    private final PriceVisualMapper priceVisualMapper;
    private final CategoryVisualMapper categoryVisualMapper;
    private final GenderVisualMapper genderVisualMapper;
    private final AgeVisualMapper ageVisualMapper;
    private final CourseTopNVisualMapper courseTopNVisualMapper;

    @Override
    public List<RateVisual> getRateVisuals() {
        QueryWrapper<RateVisual> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true,true,"rate");
        return rateVisualMapper.selectList(queryWrapper);
    }

    @Override
    public List<PriceVisual> getPriceVisuals() {
        List<PriceVisual> priceVisuals = priceVisualMapper.selectList(null);
        PriceVisual[] retList = new PriceVisual[priceVisuals.size()];
        for(int i=0;i<retList.length;i++){
            PriceVisual priceVisual = priceVisuals.get(i);
            if(priceVisual.getPrice().equals("小于500元")){
                retList[0] = priceVisual;
            }else if(priceVisual.getPrice().equals("500-1000元")){
                retList[1] = priceVisual;
            }else if(priceVisual.getPrice().equals("1000-1500元")){
                retList[2] = priceVisual;
            }else if(priceVisual.getPrice().equals("1500-2000元")){
                retList[3] = priceVisual;
            }else if(priceVisual.getPrice().equals("2000-2500元")){
                retList[4] = priceVisual;
            }else if(priceVisual.getPrice().equals("2500-3000元")){
                retList[5] = priceVisual;
            }else{
                retList[6] = priceVisual;
            }
        }
        return (List<PriceVisual>) Convert.toList(retList);
    }

    @Override
    public List<CategoryVisual> getCategoryVisuals() {
        QueryWrapper<CategoryVisual> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true,true,"num");
        return categoryVisualMapper.selectList(queryWrapper);
    }

    @Override
    public List<GenderVisual> getGenderVisuals() {
        QueryWrapper<GenderVisual> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true,true,"num");
        return genderVisualMapper.selectList(queryWrapper);
    }

    @Override
    public List<AgeVisual> getAgeVisuals() {
        QueryWrapper<AgeVisual> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true,true,"num");
        List<AgeVisual> ageVisuals = ageVisualMapper.selectList(queryWrapper);
        AgeVisual[] retList = new AgeVisual[ageVisuals.size()];
        for(int i=0;i<retList.length;i++){
            AgeVisual priceVisual = ageVisuals.get(i);
            switch (priceVisual.getAgeRange()) {
                case "小于20岁":
                    retList[0] = priceVisual;
                    break;
                case "20-30岁":
                    retList[1] = priceVisual;
                    break;
                case "30-40岁":
                    retList[2] = priceVisual;
                    break;
                case "40-50岁":
                    retList[3] = priceVisual;
                    break;
                case "50-60岁":
                    retList[4] = priceVisual;
                    break;
                case "60岁以上":
                default:
                    retList[5] = priceVisual;
                    break;
            }
        }
        return (List<AgeVisual>) Convert.toList(retList);
    }

    @Override
    public void saveOrUpdateGenderVisualBatch(List<GenderVisual> genderVisuals) {
        genderVisualMapper.saveOrUpdateGenderVisualBatch(genderVisuals);
    }

    @Override
    public List<CourseTopNVisual> getCourseTopNVisuals() {
        QueryWrapper<CourseTopNVisual> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true,false,"score");
        return courseTopNVisualMapper.selectList(queryWrapper);
    }

    @Override
    public void updateCourseTopNVisuals(List<CourseTopNVisual> topNVisuals) {
        courseTopNVisualMapper.updateCourseTopNVisuals(topNVisuals);
    }
}
